package org.equivalence.common;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;


public class FileRepository implements Serializable {	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2873942604543688026L;
	
	private ArrayList<FileVersion> files;
	private HashMap<String,FileVersion> fileMap;
	
	public FileRepository(ArrayList<String> directories,HashMap<String,Boolean> excludeFiles) {
		files = new ArrayList<FileVersion>();
		fileMap = new HashMap<String,FileVersion>();
		
		for (String directory: directories) {
			setFiles(directory,excludeFiles);
		}
	}
	
	private void setFiles(String directory,HashMap<String,Boolean> excludeFiles) {
		File newFile = new File(directory);

		FileVersion fv = new FileVersion(newFile.getPath(),newFile.lastModified());
		
		if (!excludeFiles.containsKey(newFile.getPath())) {
			files.add(fv);
			fileMap.put(newFile.getPath(), fv);
			
			if (newFile.isDirectory()) {
				File[] subFiles = newFile.listFiles();
				
				for (File f:subFiles) {
					setFiles(f.getPath(),excludeFiles);
				}
			}
		}
	}
	
	public int getFileCount() {
		return files.size();
	}
	
	public FileVersion getFile(String filePath) {
		return fileMap.get(filePath);
	}
	
	public FileVersion getFile(int i) {
		return files.get(i);
	}
	

}
